package com.example.springboot.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.springboot.dao.daoNew.TestDao1;
import com.example.springboot.dao.daoOld.TestDao2;
import com.example.springboot.dao.daoService.DaoService;
import com.example.springboot.pojo.NewItem;
import com.example.springboot.pojo.OldItem;
import com.example.springboot.pojo.SourceFile;
import com.example.springboot.pojo.UpOrIn;
import com.example.springboot.service.ArService;
import com.example.springboot.util.ReadPdf;
import com.example.springboot.util.Record;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: Wyz
 * @date: 2023/5/6 15:41
 * @return
 */
@Service
@Slf4j
public class ArServiceImpl implements ArService {

    @Resource
    private TestDao1 newDao;

    @Resource
    private TestDao2 oldDao;

    @Resource
    private DaoService serviceDao;


    @Override
    public String getsql(String archTypeName , String sectName) {
        String s = "";
        Long aLong = serviceDao.selectId(sectName);
        List<NewItem> newItems = newDao.selectNewItem(archTypeName,aLong);
        List<OldItem> oldItems = oldDao.selectOldItem(archTypeName, sectName);
        for (int i = 0; i < oldItems.size(); i++) {
            if (!StringUtils.isEmpty(oldItems.get(i).getFileNo())){
                for (int i1 = 0; i1 < newItems.size(); i1++) {
                    if (oldItems.get(i).getItemName().equals(newItems.get(i1).getItemName())
                        && oldItems.get(i).getFileNumber().equals(newItems.get(i1).getFileNumber().substring(2))
                    ){
                        Long categoryId = newItems.get(i1).getCategoryId();
                        String uuid = serviceDao.selectUUID(categoryId);
                        Long itemId = newItems.get(i1).getItemId();
                        UpOrIn upOrIn = newDao.selectPropertyValue(itemId, uuid);
                        if (ObjectUtil.isNotNull(upOrIn)){
                            if (upOrIn.getItemPropertyValueId()!=null){
                                if (StringUtils.isEmpty(upOrIn.getPropertyValue())){
                                    //修改
                                    s+= "UPDATE item_property_value " +
                                            "SET property_value = '"+oldItems.get(i).getFileNo()+"'" +
                                            "  WHERE item_property_value_id  = "+upOrIn.getItemPropertyValueId()+";\n";
                                }
                            }else {
                                //新增
                                s+= "INSERT INTO `item_property_value` ( `property_value`, `item_id`, `category_property_uuid`, `delete_flag`, `create_user`, `create_time`, `update_user`, `update_time` )\n" +
                                        "VALUES\n" +
                                        "\t( '"+oldItems.get(i).getFileNo()+"', "+itemId+", '"+uuid+"', 0, "+newItems.get(i1).getCreateUser()+", now( ), "+newItems.get(i1).getCreateUser()+", now( ) );\n\n";
                            }

                        }
                    }
                }
            }
        }

        return s;
    }

    /**
     * 查询打不开的pdf id
     *
     * @param sectName : 全宗名称
     * @return java.util.List<java.lang.Long>
     * @date 2023/5/8 14:26
     * @author wyz
     * @see List< Long>
     */
    @Override
    public List<Long> selectFileUrl(String sectName) {
        String read = Record.read(sectName);
        String[] split = read.split(",");
        List<String> split1 = Arrays.asList(split);
        List<String> newList = new ArrayList<>(split1);
        List<String> collect = newList.stream().filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
        List<Long> list = new ArrayList<>();
        Long aLong = serviceDao.selectId(sectName);
        log.info("全宗id：{}",aLong);
        List<SourceFile> sourceFiles = newDao.selectFileUrl(aLong);
        log.info("全宗下pdf文件为：{}",sourceFiles);
        log.warn("一共{}个",sourceFiles.size());
        if (CollUtil.isNotEmpty(sourceFiles)){
            for (int i = 0; i < sourceFiles.size(); i++) {
                log.warn("现在执行到第{}个",i+1);
                if (!collect.contains(sourceFiles.get(i).getFileId().toString())){
                    Record.write(sourceFiles.get(i).getFileId().toString()+",",sectName);
                    log.info("执行到：{}",sourceFiles.get(i).getFileId());
                    String pdfContent = ReadPdf.getPdfContent(sourceFiles.get(i).getFileUrl());
                    if (pdfContent.equals("出错")){
                        Record.write(sourceFiles.get(i).getFileId().toString()+",",sectName+"，出错");
                        log.info("{}:出错",sourceFiles.get(i).getFileId());
                        list.add(sourceFiles.get(i).getFileId());
                    }
                }
            }
        }
        System.err.println(sectName+"全部完成");
        return list;
    }

    /**
     * 删除重复条目
     *
     * @param sectName : 全宗名称
     * @return java.util.List<java.lang.String>
     * @date 2023/5/10 17:23
     * @author wyz
     * @see String
     */
    @Override
    public String delSql(String sectName) {
        String sql = "";
        Long aLong = serviceDao.selectId(sectName);
        List<NewItem> newItems = newDao.selectNewItem(null, aLong);
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < newItems.size(); i++) {
            System.err.println("对比到第"+(i+1)+"个；共"+newItems.size()+"个");
            for (int i1 = 0; i1 < newItems.size(); i1++) {
                if (!list.contains(newItems.get(i1).getItemId())) {
                    if (!newItems.get(i).getItemId().equals(newItems.get(i1).getItemId())){
                        if (newItems.get(i).getItemName().equals(newItems.get(i1).getItemName())&&
                                newItems.get(i).getFileNumber().equals(newItems.get(i1).getFileNumber())){
                            //查case
                            String s = newDao.selectArchiveNo(newItems.get(i).getItemId());
                            String s1 = newDao.selectArchiveNo(newItems.get(i1).getItemId());
                            if (!StringUtils.isEmpty(s)&&!StringUtils.isEmpty(s1)){
                               if (s.equals(s1)){
                                   list.add(newItems.get(i).getItemId());
                                   sql += "UPDATE item set item.delete_flag = 1 where item.item_id ="+newItems.get(i).getItemId()+" ;\n";
                               }
                            }else {
                                list.add(newItems.get(i).getItemId());
                                sql += "UPDATE item set item.delete_flag = 1 where item.item_id ="+newItems.get(i).getItemId()+" ;\n";
                            }
                        }
                    }
                }
            }
        }
        return sql;
    }

    /**
     * 删除全宗下重复原文
     *
     * @param sectName :
     * @return java.lang.String
     * @date 2023/5/12 10:50
     * @author wyz
     * @see String
     */
    @Override
    public String delSoureFile(String sectName) {
        String sql = "";
        Long aLong = serviceDao.selectId(sectName);
        List<SourceFile> sourceFiles = newDao.selectSoureFileOldId(aLong);
        int i=0;
        int j=0;
        String s = "";
        for (SourceFile aLong1 : sourceFiles) {
            j++;
            List<Long> longList1 = newDao.selectSoureFileNewId(aLong1.getOldFileId(),aLong1.getItemId());
            List<Long> longList2 = longList1.subList(0, longList1.size()-1);
            System.err.println("一共"+sourceFiles.size()+"个，执行到"+j+"个");
            i+=longList2.size();
            s+="\n"+longList1.size();
            for (Long aLong2 : longList2) {
                sql+="UPDATE source_file set delete_flag = 1 where file_id = "+aLong2+";\n";
            }
        }
        System.err.println(s);
        System.err.println(i);
        return sql;
    }

}
